﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


using System.Data.SqlClient;


namespace syc.Store
{
    public partial class 产品库存统计 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack)
            {
                this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {

            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string sql = "select count(id),sum(重量),sum(长度),sum(支数) from 产品库存 where 状态=1";
            string sql2 = "select 产品库存.[ID],产品库存.捆号,产品.品名,产品库存.生产单位ID,原料供货单位.名称 as 原料供货,产品库存.炉号,产品库存.重量,产品库存.长度,产品库存.支数,产品库存.入库日期 from 产品库存,产品,原料供货单位 where 产品.[ID]=产品库存.产品类别ID and 原料供货单位.[ID]=产品库存.原料供货ID and 产品库存.状态=1";
            int chanpin = 0;
            int fenchang = 0;
            int gonghuo = 0;
            double kunchang = 0;
            double kunzhong = 0;
            string luhao = this.LuHao.Text.Trim().Replace("'", "");
            string kunhao = this.KunHao.Text.Trim().Replace("'", "");
            DateTime d1, d2;
            int.TryParse(this.ChanPin.Text, out chanpin);
            int.TryParse(this.FenChang.Text, out fenchang);
            int.TryParse(this.GongHuo.Text, out gonghuo);
            double.TryParse(this.KunZhong.Text, out kunzhong);
            double.TryParse(this.KunChang.Text, out kunchang);
            if (chanpin != 0)
            {
                sql += " and 产品类别ID=" + chanpin;
                sql2 += " and 产品库存.产品类别ID=" + chanpin;
            }
            if (fenchang != 0)
            {
                sql += " and 生产单位ID=" + fenchang;
                sql2 += " and 产品库存.生产单位ID=" + fenchang;
            }
            if (gonghuo != 0)
            {
                sql += " and 原料供货ID=" + gonghuo;
                sql2 += " and 产品库存.原料供货ID=" + gonghuo;
            }
            if (kunzhong > 0)
            {
                sql += " and 重量=" + kunzhong;
                sql2 += " and 产品库存.重量=" + kunzhong;
            }
            if (kunchang > 0)
            {
                sql += " and 长度=" + kunchang;
                sql2 += " and 产品库存.长度=" + kunchang;
            }

            if (luhao != "")
            {
                sql += " and 炉号='" + luhao+"'";
                sql2 += " and 产品库存.炉号='" + luhao+"'";
            }

            if (kunhao != "")
            {
                sql += " and 捆号 like '" + kunhao + "%'";
                sql2 += " and 产品库存.捆号 like '" + kunhao + "%'";
            }

            if (DateTime.TryParse(Date1.Text, out d1))
            {
                sql += " and 入库日期>='" + d1.ToShortDateString() + "'";
                sql2 += " and 产品库存.入库日期>='" + d1.ToShortDateString() + "'";
            }
            else if (Date1.Text != "")
            {
                this.Label_Result.Text = "入库起始日期输入有误";
                return;
            }
            if (DateTime.TryParse(Date2.Text, out d2))
            {
                sql += " and 入库日期<='" + d2.ToShortDateString() + "'";
                sql2 += " and 产品库存.入库日期<='" + d2.ToShortDateString() + "'";
            }
            else if (Date2.Text != "")
            {
                this.Label_Result.Text = "入库截止日期输入有误";
                return;
            }

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = sql;
            Cmd.CommandType = CommandType.Text;

            SqlDataReader Reader = Cmd.ExecuteReader();

            if (Reader.Read())
            {
                if (!Reader.IsDBNull(1))
                {
                    this.Label_Result.Text = "捆数：" + (int)Reader[0] + "捆　　　　总重：" + (double)Reader[1] + "kg　　　　总长：" + (double)Reader[2] + "m　　　　总支数：" + (int)Reader[3] + "支";
                }
                else
                {
                    this.Label_Result.Text = "捆数：0捆　　　　总重：0kg　　　　总长：0m　　　　总支数：0支";
                }
            }
            Reader.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Close();
            Conn.Dispose();
            this.SqlDataSource1.SelectCommand = sql2;
            this.ViewState["SelectCommand"] = sql2;
        }
        protected void Button_All_Click(object sender, EventArgs e)
        {
            this.ChanPin.Text = "0";
            this.FenChang.Text = "0";
            this.GongHuo.Text = "0";
            this.LuHao.Text = "";
            this.Date1.Text = "";
            this.Date2.Text = "";
            this.KunHao.Text = "";
            this.KunChang.Text = "";
            this.KunZhong.Text = "";

            this.Button_Query_Click(null, null);
        }

 
    }
}
